package com.jiangjiang.eshop.membership.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jiangjiang.eshop.membership.convert.UserAccountConvert;
import com.jiangjiang.eshop.membership.dto.UserAccountDTO;
import com.jiangjiang.eshop.membership.service.MembershipService;
import com.jiangjiang.eshop.membership.service.UserAccountService;
import com.jiangjiang.eshop.membership.updater.FirstLoginMembershipUpdater;
import com.jiangjiang.eshop.membership.updater.PayOrderMembershipUpdater;
import com.jiangjiang.eshop.membership.updater.PublishCommentMembershipUpdater;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName MembershipServiceImpl
 * @Description TODO
 * @Author jhw
 * @Date 2021/6/3 21:55
 * @Version 1.0
 **/
@Slf4j
@Service
public class MembershipServiceImpl implements MembershipService {

    /**
     * 用户账户service组件
     */
    @Autowired
    private UserAccountService userAccountService;
    /**
     * 用户账户转换器
     */
    @Autowired
    private UserAccountConvert userAccountConvert;
    /**
     * 每天第一次登录会员信息更新组件
     */
    @Autowired
    private FirstLoginMembershipUpdater firstLoginMembershipUpdater;
    /**
     * 支付订单会员信息更新组件
     */
    @Autowired
    private PayOrderMembershipUpdater payOrderMembershipUpdater;
    /**
     * 发表评论会员信息更新组件
     */
    @Autowired
    private PublishCommentMembershipUpdater publishCommentMembershipUpdater;

    @Override
    public Boolean informFirstLoginDailyEvent(Long userAccountId) {
        try {
            return firstLoginMembershipUpdater.execute(userAccountId, null);
        }catch (Exception e){
            log.warn("MembershipServiceImpl informFirstLoginDailyEvent error",e);
            return false;
        }

    }

    @Override
    public Boolean informPayOrderEvent(Long userAccountId, Double totalOrderAmount) {
      try {
          return payOrderMembershipUpdater.execute(userAccountId, totalOrderAmount);
      }catch (Exception e){
          log.warn("MembershipServiceImpl informPayOrderEvent error",e);
          return false;
      }
    }

    @Override
    public Boolean informFinishReturnGoodsEvent(Long userAccountId, Double totalOrderAmount) {
        try {
            return payOrderMembershipUpdater.undo(userAccountId, totalOrderAmount);
        }catch (Exception e){
            log.warn("MembershipServiceImpl informFinishReturnGoodsEvent error",e);
            return false;
        }
    }

    @Override
    public Boolean informPublishCommentEvent(Long userAccountId, Boolean showPictures) {
       try {
           return publishCommentMembershipUpdater.execute(userAccountId, showPictures);
       }catch (Exception e){
           log.warn("MembershipServiceImpl informPublishCommentEvent error",e);
           return false;
       }
    }

    @Override
    public Boolean informRemoveCommentEvent(Long userAccountId, Boolean showPictures) {
        try {
            return publishCommentMembershipUpdater.undo(userAccountId, showPictures);
        }catch (Exception e){
            log.warn("MembershipServiceImpl informRemoveCommentEvent error",e);
            return false;
        }
    }

    @Override
    public List<UserAccountDTO> listAllUserAccounts() {
        return userAccountConvert.convertEntities2DTOList(userAccountService.list(null));
    }
}
